<?php
/**宝贝分类
 * Class AdController
 * @package Admin\Controller
 * @author leiying
 */

namespace Admin\Controller;

class CategoryController extends AdminController {

    /**获取分类的子分类id(包含该分类id)
     * @param $cat_id
     * @return array
     * @author leiying
     */
    protected  function getCategoryId($cat_id){
        $tree_list = M('goods_category')->field("id,pid,name")->order("sort asc")->select();
        $res = list_to_tree($tree_list,"id","pid","_child",$cat_id);
        $lists = tree_to_array($res);
        $ids[] = $cat_id;
        foreach($lists as $v){
            $ids[] = $v['id'];
        }
        return $ids;
    }

    /**
     * 宝贝分类
     * @author leiying
     */
    public function categorylist(){

        $get = I('post.');

        if(isset($get['cat_id']) && $get['cat_id'] != '' && isset($get['cat_name']) && $get['cat_name'] != ''){
            //树型搜索
            $tree_list = M('goods_category')->order("sort asc")->select();
            $search_data['cat_id'] = $get['cat_id'];
            $search_data['cat_name'] = $get['cat_name'];
            $res = list_to_tree($tree_list,"id","pid","_child",$search_data['cat_id']);
            $lists = tree_to_array($res);
            foreach($tree_list as $k=>$v){
                if($v['id'] == $search_data['cat_id']){
                    $v['level'] = 0;
                    $first = $v;
                    break;
                }
            }
            foreach($lists as $k=>&$v){
                $v['level'] = $v['level']+1;
            }
            array_unshift($lists,$first);
        }elseif(isset($get['cat_name']) && $get['cat_name'] != ''){
            //自定义搜索
            $where = array();
            if(isset($get['cat_name']) && $get['cat_name'] != ''){
                $search_data['cat_name'] = $get['cat_name'];
                $where['name'] = array("like",'%'.trim($get['cat_name']).'%');
            }
            $lists = M('goods_category')->where($where)->order("sort asc")->select();

        }else{
            //无搜索条件
            $tree_list = M('goods_category')->order("sort asc")->select();
            $res =list_to_tree($tree_list,"id","pid","_child",0);//处理成分级的嵌套数组
            $lists = tree_to_array($res);//处理成一维数组
        }


        foreach($lists as $k=>&$v){
            $v['xid'] = $k+1;
        }

        $this->assign('lists',$lists);
        foreach($lists as $k=>&$v){
            $v['open'] = true;
            unset($v['sort']);
            unset($v['level']);
            unset($v['url']);
            unset($v['tid']);
            unset($v['xid']);
            unset($v['status']);
            unset($v['cover_pic']);
        }
        $ztree = json_encode($lists);

        $this->assign('ztree',$ztree);

        $this->assign('searchs',$search_data);
        gc_collect_cycles();
        $get_id = I('get.id');
        if($get_id){
            $this->assign("src",U('Admin/Category/categoryEdit',array('id'=>$get_id)));
        }else{
            $this->assign("src",U('Admin/Category/categoryAdd'));
        }


        $this->display();


    }
    /**删除宝贝分类
     * @author leiying
     */
    public function delCategory(){
        if(IS_POST){
            $post_id = I('post.id');
            if(empty($post_id)){
                $this->error("请选择需要操作的数据");
            }
            //判断删除的分类是否为父级分类
            $result = M('goods_category')->where(array('pid'=>$post_id))->find();
            if($result){
                $this->error('不能删除父级分类');
            }else{
                //查询分类下是否有宝贝
                $check = M("goods")->where(array('cat_id'=>$post_id))->find();
                if($check){
                    $this->error('该分类下存在宝贝，请先删除宝贝');
                }else{
                    $res = M('goods_category')->where(array('id'=>$post_id))->delete();
                    if($res){
                        $this->success('删除成功');
                    }else{
                        $this->error('删除失败');
                    }
                }

            }
        }else{
            $this->error('非法操作');
        }
    }

    /**添加宝贝分类
     * @author leiying
     */
    public function categoryAdd(){
        $post_btn = I('post.btn');
        if($post_btn){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            //处理参数
            $pid = $post["pid"];
            $name = trim($post["name"]);
            if($name == ''){
                $this->error('分类名称不能为空');
            }
            $status = $post['status'];
            if(!in_array($status,array(1,0))){
                $this->error("请选择状态");
            }
            $sort = empty(trim($post["sort"]))?0:trim($post["sort"]);
            $relation_brand = trim($post["select_brand"],",");

            $data = array(
                'name'=>$name,
                'sort'=>$sort,
                'status'=>$status,
                'pid'=>$pid,
                'relation_brand'=>$relation_brand
            );


            //验证通过了
            $res = M('goods_category')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $typeList = M('goods_category')->where(array('status'=>1))->order("sort asc")->select();
            $res =list_to_tree($typeList);//处理成分级的嵌套数组
            //处理成一维数组
            $pids = tree_to_array($res);
            $this->assign('pids',$pids);
            $this->display();
        }
    }

    /**修改宝贝分类
     * @author leiying
     */
    public function categoryEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            //处理参数
            $pid = $post["pid"];
            $name = trim($post["name"]);
            if($name == ''){
                $this->error('分类名称不能为空');
            }
            $status = $post['status'];
            if(!in_array($status,array(1,0))){
                $this->error("请选择状态");
            }
            $sort = empty(trim($post["sort"]))?0:trim($post["sort"]);
            $relation_brand = trim($post["select_brand"],",");

            $data = array(
                'name'=>$name,
                'sort'=>$sort,
                'status'=>$status,
                'pid'=>$pid,
                'relation_brand'=>$relation_brand
            );

            $res = M('goods_category')->where(array('id'=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $id = I('get.id');
            if($id){
                $data = M('goods_category')->where(array('id'=>$id))->find();
                $this->assign('data',$data);

                $ids = explode(',',$data["relation_brand"]);

                if($ids){
                    $brandlist = M("brand")->field("brand_id,title")->where(array("brand_id"=>array("in",$ids)))->order("FIND_IN_SET(brand_id,'".$data["relation_brand"]."')")->select();
                }else{
                    $brandlist = array();
                }
                $brand_count = count($brandlist);
                $this->assign('brand_count',$brand_count);

                $this->assign("brandlist",$brandlist);
                $typeList = M('goods_category')->where(array('status'=>1,'id'=>array('neq',$id)))->order("sort asc")->select();
                $res =list_to_tree($typeList);//处理成分级的嵌套数组
                //处理成一维数组
                $pids = tree_to_array($res);
                $this->assign('pids',$pids);
                $this->display();
            }
        }
    }

    /**修改宝贝分类状态
     * @author leiying
     */
    public function updateTypeStatus(){
        if(IS_POST){
            $data["status"] = intval($_POST["status"]);
            $map["id"] = intval($_POST["id"]);
            $result = M('goods_category')->where($map)->save($data);
            if(is_numeric($result)){
                $this->success("执行成功");
            }else{
                $this->error("执行失败");
            }
        }else{
            $this->error('非法操作');
        }
    }
    /**ajax获取宝贝分类
     * @author leiying
     */
    public function ajax_category(){
        $typeList = M('goods_category')->field("id,pid,name")->where(array("status"=>1))->select();
        $res =list_to_tree($typeList);//处理成分级的嵌套数组
        //处理成一维数组
        $lists = tree_to_array($res);
        foreach($lists as $k=>&$v){
            $v['pId'] = $v['pid'];
        }
        $this->ajaxReturn($lists,'JSON');
    }


    /**整数或小数二位
     * @param $num
     * @param string $desc
     * @param string $type
     * @return bool
     * @author leiying
     */
    public function checkNum($num,$desc='',$type='0'){
        if(preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $num)){
            if($type == '1'){//页面发出的验证请求
                $this->success('符合条件');
            }else{
                return true;
            }
        }else{
            $this->error($desc."请输入整数或小数二位");
        }
    }

    /**整数
     * @param $num
     * @param string $desc
     * @param string $type
     * @return bool
     * @author leiying
     */
    public function checkIntNum($num,$desc='',$type='0'){
        if(preg_match('/^[0-9]+?$/', $num)){
            if($type == '1'){//页面发出的验证请求
                $this->success('符合条件');
            }else{
                return true;
            }
        }else{
            $this->error($desc."请输入整数");
        }
    }


}
